package com.scau.beyondboy.geoquiz;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.scau.beyondboy.model.TrueFalse;


public class MainActivity extends ActionBarActivity
{
    private static final String KEY_INDEX ="index";
    private static final String TAG = "MainActivity";
    private Button mTrueButton;
    private Button mFalseButton;
    private Button mNextButton;
    private TextView mQuestionTextView;
    private Button mCheatButton;
    private int mCurrentIndex=0;
    private boolean mIsCheater;
    private TrueFalse[] mQestionBlank=new TrueFalse[]
            {
            new TrueFalse(R.string.question_mideast,false),
            new TrueFalse(R.string.question_africa,false),
            new TrueFalse(R.string.question_americas,true),
            new TrueFalse(R.string.question_asia,true)
    };
    //显示用户所按的按钮
    private void checkAnswer(boolean userPressedTrue)
    {
        Log.i(TAG,"显示用户所按的按钮");
        boolean answerIsTrue=mQestionBlank[mCurrentIndex].isTrueQuestion();
        int messageId= 0b0;
        if(mIsCheater)
        {
            if(userPressedTrue==answerIsTrue)
                messageId=R.string.judment_toast;
            else
                messageId=R.string.incorrect_judgement_toast;
        }
        else
        {
            if (userPressedTrue==answerIsTrue)
            {
                messageId = R.string.correct_toast;
            } else
            {
                messageId = R.string.incorrect_toast;
            }
        }
        Toast.makeText(this,messageId,Toast.LENGTH_SHORT).show();
    }
    //更新文本内容
    private void updateQuestion()
    {
        Log.i(TAG,"更新文本内容");
        mQuestionTextView.setText(mQestionBlank[mCurrentIndex].getQuestion());
    }
    @Override
    public void onStart()
    {
        super.onStart();
        Log.i(TAG, "onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "onResume");
    }
    @Override
    public void onPause()
    {
        super.onPause();
        Log.i(TAG, "onPause");
    }
    @Override
    public void onStop()
    {
        super.onStop();
        Log.i(TAG, "onStop");
    }
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        Log.i(TAG, "onDestroy");
    }
    @TargetApi(11)
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        Log.i(TAG, "加载界面");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB)
        {
            ActionBar actionBar = getSupportActionBar();
//            if(actionBar!=null)
            actionBar.setSubtitle("Bodies of Water");
        }
        mIsCheater=false;
        mTrueButton=(Button)findViewById(R.id.true_button);
        mFalseButton=(Button)findViewById(R.id.false_button);
        mQuestionTextView=(TextView)findViewById(R.id.question_tv);
        mNextButton=(Button)findViewById(R.id.next_button);
        mCheatButton=(Button)findViewById(R.id.cheat_button);
        if(savedInstanceState!=null)
        mCurrentIndex=savedInstanceState.getInt(KEY_INDEX,0);
        updateQuestion();
        mNextButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                mIsCheater=false;
                mCurrentIndex = (mCurrentIndex + 1) % mQestionBlank.length;
                updateQuestion();
            }
        });
        mTrueButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                checkAnswer(true);
            }
        });
        mFalseButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                checkAnswer(false);
            }
        });
        //作弊查看答案
        mCheatButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Log.i(TAG,"作弊查看答案");
                Intent intent=new Intent(MainActivity.this,CheatActivity.class);
                boolean answerIsTrue=mQestionBlank[mCurrentIndex].isTrueQuestion();
                intent.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE,answerIsTrue);
                startActivityForResult(intent,0);
            }
        });
    }
    //CheatActivity返回获取数据
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        Log.i(TAG,"CheatActivity返回获取数据");
        if(data!=null)
        {
            mIsCheater=data.getBooleanExtra(CheatActivity.EXTRA_ANSWER_SHOWN,false);
        }
    }
    //保存临时的数据
    @Override public void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        Log.i(TAG,"保存临时的数据");
        outState.putInt(KEY_INDEX,mCurrentIndex);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings)
        {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
